package com.zeei.ems.meet.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zeei.ems.base.dao.StationPointDao;
import com.zeei.ems.base.page.PageParamVO;
import com.zeei.ems.base.tree.TreeNodeVO;
import com.zeei.ems.base.util.StringUtil;
import com.zeei.ems.base.util.TreeUtil;
import com.zeei.ems.base.vo.StationPointVO;
import com.zeei.ems.meet.dao.VideoDao;
import com.zeei.ems.meet.hik.NmsGetRequest;
import com.zeei.ems.meet.model.PointVideoVO;
import com.zeei.ems.meet.model.VideoVO;
import com.zeei.ems.meet.model.vo.VideoParam;
import com.zeei.ems.meet.service.VideoService;
import com.zeei.ems.meet.util.BeanMapperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class VideoServiceImpl implements VideoService {
    @Autowired
    VideoDao videoDao;
    @Autowired
    StationPointDao stationPointDao;
    //@Autowired
    //HikVisionVideoNmsClient hikVisionVideoNmsClient;

    @Override
    public PageInfo<VideoVO> listVideo(PageParamVO page, VideoParam videoParam) {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<VideoVO> videoList = videoDao.listVideo(videoParam);
        return new PageInfo<>(videoList);
    }

    @Override
    public List<VideoVO> listVideo(VideoParam videoParam) {
        return videoDao.listVideo(videoParam);
    }

    @Override
    public List<TreeNodeVO<PointVideoVO>> pointVideoList() {
        List<PointVideoVO> pointVideoList = new ArrayList<>();
        Map<String, Object> condition = new HashMap<>();
        List<StationPointVO> stationPointList = stationPointDao.queryStationPointByCondition(condition);
        if (!CollectionUtil.isEmpty(stationPointList)){
            VideoParam videoParam = new VideoParam();
            List<VideoVO> videoList = videoDao.listVideo(videoParam);
            NmsGetRequest getRequest = new NmsGetRequest();
            getRequest.setPageNo(1);
            getRequest.setPageSize(1000);
            //ListResponse<CameraOnlineDto> cameraOnline = hikVisionVideoNmsClient.getCameraOnline(getRequest);
            //List<CameraOnlineDto> cameraOnlineList = cameraOnline.getData().getList();

            stationPointList.forEach(point -> {
                PointVideoVO pointVideo = new PointVideoVO();
                pointVideo.setIsPoint(1);
                pointVideo.setParentId("0");
                pointVideo.setId(point.getPointCode().toString());
                pointVideo.setText(point.getPointName());
                pointVideoList.add(pointVideo);
                List<VideoVO> collect = videoList.stream().filter(
                        video -> video.getPointCode().equals(point.getPointCode())).collect(Collectors.toList());
                if (!CollectionUtil.isEmpty(collect)){
                    collect.forEach(video -> {
                        PointVideoVO pointVideoItem = BeanMapperUtils.map(video, PointVideoVO.class);
                        pointVideoItem.setIsPoint(0);
                        pointVideoItem.setParentId(point.getPointCode().toString());
                        pointVideoItem.setId(String.format("%s_%s", point.getPointCode(), video.getVideoId()));
                        pointVideoItem.setText(video.getMonitorName());
                        // 判断视频是否在线 status = 0 代表离线 status = 1 代表在线
                        pointVideoItem.setStatus(video.getVideoStatus());

                        /*List<CameraOnlineDto> cameraList = cameraOnlineList.stream().filter(
                                camera -> camera.getIndexCode().equals(video.getIChannelId())).collect(Collectors.toList());
                        if (!CollectionUtil.isEmpty(cameraList)){
                            CameraOnlineDto cameraOnlineDto = cameraList.get(0);
                            if (cameraOnlineDto == null || cameraOnlineDto.getOnline() == null){
                                pointVideoItem.setStatus(0);
                            }else{
                                pointVideoItem.setStatus(cameraOnlineDto.getOnline());
                            }
                        }else{
                            pointVideoItem.setStatus(0);
                        }*/
                        pointVideoList.add(pointVideoItem);
                    });
                }
            });
        }

        return TreeUtil.buildTreeNodeTree(pointVideoList, "");
    }

    @Override
    public VideoVO getVideoConfig() {
        VideoVO videoVO = null;
        VideoParam videoParam = new VideoParam();
        List<VideoVO> videoList = videoDao.listVideo(videoParam);
        for (VideoVO video : videoList) {
            if (StringUtil.isNotEmpty(video.getAccessName()) &&
                StringUtil.isNotEmpty(video.getAccessPwd())  &&
                StringUtil.isNotEmpty(video.getVideoAddress()) &&
                StringUtil.isNotEmpty(video.getVideoPort())) {
                videoVO = video;
                break;
            }
        }

        return videoVO;
    }

    @Override
    public Boolean addVideo(VideoVO video) {
        Integer integer = videoDao.addVideo(video);
        return integer > 0 ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public Boolean updateVideo(VideoVO video) {
        Integer integer = videoDao.updateVideo(video);
        return integer > 0 ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public Boolean deleteVideo(List<Integer> videoIdList) {
        Integer integer = videoDao.deleteVideo(videoIdList);
        return integer > 0 ? Boolean.TRUE : Boolean.FALSE;
    }
}
